<?php
	include_once("Database.inc");
	
class PayStation {
	
	public $ministryId;
	public $deptCode;
	public $payStationCode;
	public $description;
	public $regionCode;
	public $provinceCode;
	public $districtCode;
	
	/*
	 * Obtains an array of all pay station records for the specified ministry
	 * and department.
	 */
	 public static function getAllPayStations($min, $dep) {
	 	$ps = array();
	 	
	 	$query = $GLOBALS["DB"] -> 
	 		doQuery("SELECT MINISTRYID, DEPTCODE, PAYSTATIONCODE, PAYSTATIONDESC, REGIONCODE, PROVINCECODE, DISTRICTCODE ".
					"FROM PAYSTATIONS WHERE MINISTRYID=$min AND DEPTCODE=$dep");
	 	
	 	while ($row = mssql_fetch_array($query)) {
	 		array_push($ps, PayStation::getPayStationFromArray($row));		
	 	}
	 	
	 	return $ps;
	 }
	 
	 /*
	  * Obtains the PayStation with the specified ministry, department and code.
	  */
	  public static function getPayStation($ministryId, $depId, $code) {
	  	$txtQuery = "SELECT MINISTRYID, DEPTCODE, PAYSTATIONCODE, PAYSTATIONDESC, REGIONCODE, PROVINCECODE, DISTRICTCODE ".
					"FROM PAYSTATIONS WHERE MINISTRYID=$ministryId AND DEPTCODE=$depId AND PAYSTATIONCODE=$code";
	  	
	  	$query = $GLOBALS["DB"] -> doQuery($txtQuery);
	  		
	  	if ($row = mssql_fetch_array($query)) {
	  		return PayStation::getPayStationFromArray($row);
	  	} else {
	  		return null;
	  	}
	  }
	  
	 /*
	  * Updates the specified pay station.
	  */
	 public static function updatePayStation($ps) {
	 	$txtQuery = 
	 		"UPDATE PAYSTATIONS SET PROVINCECODE='".$ps->provinceCode."', DISTRICTCODE='".$ps->districtCode."', ".
			"PAYSTATIONDESC='".str_replace("'", "''", $ps->description)."', REGIONCODE='".$ps->regionCode."' ".
	 		"WHERE MINISTRYID='".$ps->ministryId."' AND DEPTCODE='".$ps->deptCode."' AND PAYSTATIONCODE='".$ps->payStationCode."'";
	 	
	 	$query = $GLOBALS["DB"] -> doQuery($txtQuery);
	 }

	 /*
	  * Insert a new pay station.
	  */
	 public static function newPayStation($ps) {
	 	$txtQuery = 
	 		"INSERT INTO PAYSTATIONS (PROVINCECODE, DISTRICTCODE, MINISTRYID, DEPTCODE, PAYSTATIONCODE, ".
			"PAYSTATIONDESC, REGIONCODE) VALUES ('$ps->provinceCode', '$ps->districtCode', '$ps->ministryId', ".
	 		"'$ps->deptCode', '$ps->payStationCode','".str_replace("'", "''", $ps->description)."', '$ps->regionCode')";

	 	$query = $GLOBALS["DB"] -> doQuery($txtQuery);
	 }

	/*
	 * Creates a new PayStation object from an array record.
	 */
	 protected static function getPayStationFromArray($array) {
	 	$p = new PayStation();
	 	
	 	$p->ministryId = $array[0];
	 	$p->deptCode = $array[1];
	 	$p->payStationCode = $array[2];
	 	$p->description = $array[3];
	 	$p->regionCode = $array[4];
	 	$p->provinceCode = $array[5];
	 	$p->districtCode = $array[6];
	 	
	 	return $p;
	 }
	
}	
	
	
?>